'''
    @project: Masdevallia
    @author: Austin Baker
    @created: 2/22/2014
    @license: http://opensource.org/licenses/mit-license.php

    The MIT License (MIT)

    Copyright (c) 2014 Austin Baker
    
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
'''

'''
============================================================
--->>   IMPORT MODULES
============================================================
'''
import os, time, stat
import masdevallia.settings.PathConstants as Paths

'''
============================================================
--->>   UTILITY METHODS
============================================================
'''
def searchDir( inDir, inFileType ):
    '''
    Searches the directory for files of the given type.
    
    @inDir: String. Path of the directory to search.
    @inFileType: String. File type extension. No need for the '.'
    @return: List. Full path and filename string.
    '''
    filelist = []
    for file in os.listdir( inDir ):
        if file.endswith( inFileType ):
            filelist.append( os.path.join( inDir, file ) )
    return filelist

def searchSubDir( inRootDir, inFileType ):
    '''
    Searches the root directory and all sub-directories for files of
    the given type. This will skip over any directory that starts with '.'.
    
    @inRootDir: String. Path of the root directory to search.
    @inFileType: String. File type extension. No need for the '.'
    @return: Dict. Key is the directory path. Value is the filename with extension.
    '''
    filelist = {}
    filelist[ inRootDir ] = []
    for dirpath, dirnames, filenames in os.walk( inRootDir ):
        # Remove the SVN directories.
        dirnames[:] = [ d for d in dirnames if not d[0] == '.' ]
        if dirpath not in filelist:
            filelist.setdefault( dirpath, [] )
        for filename in [ f for f in filenames if f.endswith( inFileType ) ]:
            filelist[ dirpath ].append( filename )
    return filelist

def cleanModulePYC( inProject ):
    '''
    Removes all outdated PYC files. This is to make sure the user
    has the latest compiled Python files. Sometimes Maya doesn't
    delete outdated files when it starts up.
    
    @inProject: Root directory name for the Python project.
    '''    
    path = os.path.join( Paths.PATH_PYTHON_MODULES, inProject )
    files = searchSubDir( path, 'pyc' )
    for key, value in files.items():
        for filename in value:
            pycPath = os.path.join( key, filename )        
            pyPath = os.path.join( key, filename[:-1] )
            if os.path.exists(pyPath):
                pyTimestamp = time.gmtime( os.path.getmtime( pyPath ) )
                pycTimestamp = time.gmtime( os.path.getmtime( pycPath ) )
                if pycTimestamp < pyTimestamp:
                    os.chmod( pycPath, stat.S_IWRITE )
                    os.remove( pycPath )